package com.cn.xact.ty_treasured_book.activity;

import java.util.ArrayList;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.R.layout;
import com.cn.xact.ty_treasured_book.R.menu;
import com.cn.xact.ty_treasured_book.activity.MainActivity.MainPageAdapter;
import com.cn.xact.ty_treasured_book.activity.MainActivity.MainPageChangeListener;

import android.os.Bundle;
import android.os.Parcelable;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class PhoneGuideActivity extends Activity {

    private ViewPager viewPager;
    private ArrayList<ViewGroup> pageViews;
    private ViewGroup main;

    TextView title1;
    TextView separator1;
    TextView title2;
    TextView pageIndicator;

    private String[] mPhoneGuideEntries;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        LayoutInflater inflater = getLayoutInflater();
        pageViews = new ArrayList<ViewGroup>();

        initFrames(inflater);

        main = (ViewGroup)inflater.inflate(R.layout.activity_phone_guide, null);

        viewPager = (ViewPager)main.findViewById(R.id.guidePages);
        pageIndicator = (TextView)main.findViewById(R.id.phone_guide_page_num);

        setContentView(main);

        viewPager.setAdapter(new PhoneGuidePageAdapter());
        viewPager.setOnPageChangeListener(new PhoneGuidePageChangeListener());

        mPhoneGuideEntries = getResources().getStringArray(R.array.phone_guide_entry);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.phone_guide);
        title1 = (TextView)findViewById(R.id.common_title_first_section);
        title1.setText(R.string.guide_title);
        separator1 = (TextView)findViewById(R.id.common_title_first_separator);
        separator1.setVisibility(View.VISIBLE);
        title2 = (TextView)findViewById(R.id.common_title_second_section);
        title2.setText(R.string.guide_title_mail);
        title2.setVisibility(View.VISIBLE);

        pageIndicator.setText("1/" + pageViews.size());
    }

    private void initFrames(LayoutInflater inflater) {

        ViewGroup frame1 = (ViewGroup)inflater.inflate(R.layout.common_frame_page, null);
        ViewGroup frame2 = (ViewGroup)inflater.inflate(R.layout.common_frame_page, null);
        ViewGroup frame3 = (ViewGroup)inflater.inflate(R.layout.common_frame_page, null);
        ViewGroup frame4 = (ViewGroup)inflater.inflate(R.layout.common_frame_page, null);
        ViewGroup frame5 = (ViewGroup)inflater.inflate(R.layout.common_frame_page, null);
        ViewGroup frame6 = (ViewGroup)inflater.inflate(R.layout.common_frame_page, null);
        ViewGroup frame7 = (ViewGroup)inflater.inflate(R.layout.common_frame_page, null);

        String[] topics = getResources().getStringArray(R.array.phone_guide_common_frame_topics);

        initItemsforFrame(frame1, R.drawable.phone_guide_mail_ad, topics, new Intent(PhoneGuideActivity.this, PhoneGuideForMailActivity.class));
        initItemsforFrame(frame2, R.drawable.phone_guide_sms, topics, new Intent(PhoneGuideActivity.this, PhoneGuideForMailActivity.class));
        initItemsforFrame(frame3, R.drawable.phone_guide_security, topics, new Intent(PhoneGuideActivity.this, PhoneGuideForMailActivity.class));
        initItemsforFrame(frame4, R.drawable.phone_guide_camera, topics, new Intent(PhoneGuideActivity.this, PhoneGuideForMailActivity.class));
        initItemsforFrame(frame5, R.drawable.phone_guide_mail_ad, topics, new Intent(PhoneGuideActivity.this, PhoneGuideForMailActivity.class));
        initItemsforFrame(frame6, R.drawable.phone_guide_mail_ad, topics, new Intent(PhoneGuideActivity.this, PhoneGuideForMailActivity.class));
        initItemsforFrame(frame7, R.drawable.phone_guide_mail_ad, topics, new Intent(PhoneGuideActivity.this, PhoneGuideForMailActivity.class));

        pageViews.add(frame1);
        pageViews.add(frame2);
        pageViews.add(frame3);
        pageViews.add(frame4);
        pageViews.add(frame5);
        pageViews.add(frame6);
        pageViews.add(frame7);
    }

    private void initItemsforFrame(ViewGroup commonFrame, int adResourceId, String[] buttonText, final Intent intent) {
        WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        int width = (wm.getDefaultDisplay().getWidth() * 1/2);
        int height = (wm.getDefaultDisplay().getWidth() * 2/9);

        ImageView ad = (ImageView) commonFrame.findViewById(R.id.common_ads);
        ad.setBackgroundResource(adResourceId);
        ad.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                startActivity(intent);
            }

        });
        Button Bt1 = (Button)commonFrame.findViewById(R.id.common_frames_item1);
        Bt1.setWidth(width);
        Bt1.setHeight(height);
        Bt1.setText(buttonText[0]);
        Button Bt2 = (Button)commonFrame.findViewById(R.id.common_frames_item2);
        Bt2.setWidth(width);
        Bt2.setHeight(height);
        Bt2.setText(buttonText[1]);
        Button Bt3 = (Button)commonFrame.findViewById(R.id.common_frames_item3);
        Bt3.setWidth(width);
        Bt3.setHeight(height);
        Bt3.setText(buttonText[2]);
        Button Bt4 = (Button)commonFrame.findViewById(R.id.common_frames_item4);
        Bt4.setWidth(width);
        Bt4.setHeight(height);
        Bt4.setText(buttonText[3]);
        Button Bt5 = (Button)commonFrame.findViewById(R.id.common_frames_item5);
        Bt5.setWidth(width);
        Bt5.setHeight(height);
        Bt5.setText(buttonText[4]);
        Button Bt6 = (Button)commonFrame.findViewById(R.id.common_frames_item6);
        Bt6.setWidth(width);
        Bt6.setHeight(height);
        Bt6.setText(buttonText[5]);
        Button Bt7 = (Button)commonFrame.findViewById(R.id.common_frames_item7);
        Bt7.setWidth(width);
        Bt7.setHeight(height);
        Bt7.setText(buttonText[6]);
        Button Bt8 = (Button)commonFrame.findViewById(R.id.common_frames_item8);
        Bt8.setWidth(width);
        Bt8.setHeight(height);
        Bt8.setText(buttonText[7]);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.phone_guide, menu);
        return true;
    }

    class PhoneGuidePageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pageViews.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getItemPosition(Object object) {
            // TODO Auto-generated method stub
            return super.getItemPosition(object);
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            // TODO Auto-generated method stub
            ((ViewPager) arg0).removeView(pageViews.get(arg1));
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            // TODO Auto-generated method stub
            ((ViewPager) arg0).addView(pageViews.get(arg1));
            return pageViews.get(arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub

        }
    }

    class PhoneGuidePageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
            int pageNum = arg0 + 1;
            pageIndicator.setText(pageNum + "/" + pageViews.size());
            title2.setText(mPhoneGuideEntries[arg0]);
        }

    }

}
